﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using urBook.Engine;

namespace urBook.Gui
{
	/// <summary>
	/// Interaction logic for GroupItem.xaml
	/// </summary>
	public partial class GroupItem : UserControl
	{
		public Group group{ get; set; }
        public MainWindow root { get; set; }

		public GroupItem(MainWindow root, Group group)
		{
			this.InitializeComponent();
            this.root = root;
            
            this.group = group;
            GroupName.Content = group.name;
            GroupRename.Text = group.name;
            if (group.imageUrl != "")
                setImageSource(GroupImage, group.imageUrl);

			GroupName.MouseDoubleClick += new System.Windows.Input.MouseButtonEventHandler(GroupName_MouseDoubleClick);
			GroupRename.KeyDown += new System.Windows.Input.KeyEventHandler(GroupRename_KeyDown);
            delete.MouseDown += new MouseButtonEventHandler(delete_MouseDown);
		}

        private void setImageSource(Image image, string source)
        {
            BitmapImage src = new BitmapImage();
            src.BeginInit();
            src.UriSource = new Uri(source, UriKind.RelativeOrAbsolute);
            src.CacheOption = BitmapCacheOption.OnLoad;
            src.EndInit();
            image.Source = src;
            image.Stretch = Stretch.Uniform;
        }

        void delete_MouseDown(object sender, MouseButtonEventArgs e)
        {
            String message = "Contacts inside this group will still remain in your Address Book. Are you sure that you would like to delete this Group?";
            String caption = "Delete " + group.name;
            MessageBoxResult result = MessageBox.Show(message, caption, MessageBoxButton.YesNo, MessageBoxImage.Question);

            if (result == MessageBoxResult.Yes)
            {
                root.addressBook.remove(group);
                root.GroupsList.Items.Remove(this);
            }
        }

		private void GroupName_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
            GroupName.Visibility = Visibility.Collapsed;
            GroupRename.Visibility = Visibility.Visible; 
        }

		private void GroupRename_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
		{
			if(e.Key == Key.Return)
			{
				GroupName.Content = GroupRename.Text;
				group.name = GroupRename.Text;
				GroupRename.Visibility = Visibility.Collapsed;
				GroupName.Visibility = Visibility.Visible;
			}
		}

        private void GroupImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName = "image"; // Default file name
            dlg.DefaultExt = ".jpg"; // Default file extension
            dlg.Filter = "Images (*.BMP;*.JPG;*.PNG)|*.BMP;*.JPG;*.PNG|" + "All files (*.*)|*.*";

            Nullable<bool> result = dlg.ShowDialog();
            if (result == true)
            {
                string filename = dlg.FileName;
                string[] splits = filename.Split(new char[] { '.' });
                string ext = splits[splits.Length - 1];
                string imagePath = root.profilePath + "\\images\\groups\\" + group.name + "." + ext;
                System.IO.File.Copy(filename, imagePath, true);
                setImageSource(GroupImage, imagePath);
                group.imageUrl = imagePath;
            }
        }

        private void Rename(object sender, RoutedEventArgs e)
        {
            GroupName.Visibility = Visibility.Collapsed;
            GroupRename.Visibility = Visibility.Visible; 
        }

        private void Delete(object sender, RoutedEventArgs e)
        {
            String message = "Contacts inside this group will still remain in your Address Book. Are you sure that you would like to delete this Group?";
            String caption = "Delete " + group.name;
            MessageBoxResult result = MessageBox.Show(message, caption, MessageBoxButton.YesNo, MessageBoxImage.Question);

            if (result == MessageBoxResult.Yes)
            {
                root.addressBook.remove(group);
                root.GroupsList.Items.Remove(this);
            }
        }

        private void ApplyGroupPhoto(object sender, RoutedEventArgs e)
        {
            group.applyPhoto();
            root.loadData();
            root.GroupsList.SelectedItem = this;
        }

        private void GroupRename_MouseDown(object sender, MouseButtonEventArgs e)
        {
            GroupName.Content = GroupRename.Text;
            group.name = GroupRename.Text;
            GroupRename.Visibility = Visibility.Collapsed;
            GroupName.Visibility = Visibility.Visible;
        }
	}
}